<?php
/**
 * User: zhangyu
 * Date: 2022/3/17
 * Time: 9:27
 */

declare (strict_types = 1);
namespace app\admin\controller;

use app\admin\model\AdminModel;
use app\admin\model\RoleModel;
use app\admin\services\AdminService;
use app\admin\validate\PhoneValidate;
use app\BaseController;
use think\App;
use think\captcha\Captcha;
use think\Exception;
use think\exception\ValidateException;

class Admin extends BaseController
{
    protected $middleware = [
        'CheckTokenMiddleware' => [
            'except' => [
                'login', 'verify', 'sendPhoneSms', 'mobileLogin', "actionVerify"
            ]
        ],
    ];
    /**
     * Login constructor.
     * @param App $app
     * @param AdminService $services
     */
    public function __construct(App $app, AdminService $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    protected function initialize()
    {
        // TODO: Implement initialize() method.
    }

    /**
     * 动态验证码
     * @return $this|\think\Response
     */
    public function actionVerify()
    {
        api_return(200, "", [
            "style" => env("ACTION_CAPTCHA.STYLE", 1),
            "wx_login" => env("LOGIN.WX_LOGIN", 0),
            "verify" => app()->make(Captcha::class)->create(null, true),
        ]);
    }

    /**
     * 账号密码登录
     * @return mixed
     */
    public function login()
    {
        $p = $this->filterParam;
        $account = $p['account']??"";
        $password = $p['password']??"";

        if (empty($p['img_code']) || !app()->make(Captcha::class)->check($p['img_code'])) {
            api_return(20003);
        }

        $this->services->checkParamRule($account, $password);
        api_return(200, "", $this->services->login($account, $password));
    }

    /**
     * 短信验证码
     */
    public function sendPhoneSms()
    {
        $p = $this->request->param();
        try {
            validate(PhoneValidate::class)->scene('phone')->check($p);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            api_return(50001, $e->getError());
        }
        $code = $this->services->sendPhoneSms($p['phone']);
        api_return(200, "", ["code" => $code]);
    }

    /**
     * 手机登录
     */
    public function mobileLogin()
    {
        $p = $this->request->param();
        $phone = $p['phone']??"";
        $code = $p['code']??"";
        try {
            validate(PhoneValidate::class)->scene('login')->check($p);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            api_return(50001, $e->getError());
        }
        api_return(200, "", $this->services->mobileLogin($phone, $code));
    }

    /**
     * 密码修改周期
     */
    public function pwdExpire()
    {
        if ($this->request->adminUid) {
            $uid = $this->request->adminUid;
            $pwdExpireBool = $this->services->pwdExpire($uid);
            api_return(200, "", ['pwdExpire' => $pwdExpireBool]);
        }
        api_return(50001);
    }

    public function info(){
//        $this->request->adminUid;
        $adminInfo=AdminModel::find($this->request->adminUid);
        $role=RoleModel::find($adminInfo->role);
        $return=[
            'avatar'=>$this->request->scheme().'://'.$this->request->host().'/static/upload/'.$adminInfo->avatar,
            'username'=>$adminInfo->nickname,
            'roles'=>explode('-',$role->permission),
            'ability'=>[
                'READ'
            ]
        ];
        api_return(200,'',$return);
    }

    public function logout(){
        api_return(200,'success',[]);
    }
}
